
const SMACK_TINYURL_ENABLE = "extensions.swaksmack.tinyurl_enable_boolpref1";
const SMACK_TINYURL_THRESHOLD = "extensions.swaksmack.tinyurl_maxurllength_intpref";

const _chatPostRegexStr = "(http:\\/\\/[^\"'\\s>]*\\/?([^\"'\\s>]*)?)";
const _chatPostRegEx = new RegExp(_chatPostRegexStr, "ig");

var _literals = /([-+*?\.()\[\]\\$^!])/g;
 
 String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function getStringTableText(key) {
	var jellyfishstrings = document.getElementById("swaksmack-strings");
	return jellyfishstrings.getString(key);
}

function getPreference(pref_name)
{
  var pref_type = prefs.getPrefType(pref_name);
  if (pref_type == prefs.PREF_STRING)
      return prefs.getCharPref(pref_name);
  else if (pref_type == prefs.PREF_INT)
      return prefs.getIntPref(pref_name);
  else if (pref_type == prefs.PREF_BOOL)
      return prefs.getBoolPref(pref_name);
  else  // fallback on error
    return prefs.PREF_INVALID;
}


function Swak_getChatPostUrlText(postText) {
	var matches	= null;
	var results = new Array();
	var numMatch = 0;
	while ((matches = _chatPostRegEx.exec(postText)) != null) {
		//check if url end with :.!, and remove
		//alert('matches[1]' + matches[1]);
 		var newurl = matches[1].replace(/[\.!,;:]$/i,'');
 		results[numMatch++] = newurl;
	}
	
 	return results;
}

//converts regex literals characters in the parameter 
function escapeRegExLiterals(plaintext) {
	if (plaintext == null) return null;
	var converted = plaintext.replace(_literals,"[$1]");
	return converted
}

function openOptions() {
	alert('openOptions');
	    window.openDialog(
        "chrome://swaksmack/content/options.xul", 
        "options",
        "chrome, dialog, centerscreen");
}